package cj.web.portal.controllers;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.constants.CategoryEnum;
import cj.web.admin.constants.KDAddressAreaEnum;
import cj.web.admin.constants.KDAddressStreetEnum;
import cj.web.admin.constants.KDBookingPaperTypeEnum;
import cj.web.admin.constants.KDBookingStatusEnum;
import cj.web.admin.constants.KDInTypeEnum;
import cj.web.admin.constants.KDRecovePaperWeightEnum;
import cj.web.portal.dao.ConfigDAO;
import cj.web.portal.dao.KDBookingDAO;
import cj.web.portal.dao.LineDAO;
import cj.web.portal.utils.ConfigConstants;

/**
 * 快递订报业务
 * @author Administrator
 *
 */
@Path("qb")
public class KDBookingController {

	private static Logger logger = Logger.getLogger(KDBookingController.class.getName());
	
	private static final int wuhanCity = 0;
	
	@Autowired
	private KDBookingDAO kdBookingDAO;
	@Autowired
	private ConfigDAO configDAO;
	@Autowired
	private LineDAO lineDAO;
	
	/**
	 * 在线订报
	 * @param model
	 * @return
	 */
	@Get("showOnlineBooking")
	public String showOnlineBooking(Model model,@Param("msg") String msg)
	{
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		model.add("memu", "showOnlineBooking");
		model.add("currentDate", DateUtils.getNowTime(DateUtils.DATE_SMALL_STR));
		model.add("msg", msg);
		model.add("kdAddressAreaEnum", EnumSet.allOf(KDAddressAreaEnum.class));
		model.add("kdAddressStreetEnum", EnumSet.allOf(KDAddressStreetEnum.class));
		model.add("kdBookingPaperTypeEnum", EnumSet.allOf(KDBookingPaperTypeEnum.class));
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/kdgs/booking/onlineBooking.vm";
	}
	
	/**
	 * 提交保存在线订报
	 * @param type				报刊类型
	 * @param beginAt			订报时间
	 * @param subPeriod			期限
	 * @param subNum			份数
	 * @param link				联系人
	 * @param phone				联系时间
	 * @param invoiceTitle		发票抬头
	 * @param city				市
	 * @param area				区
	 * @param street			街
	 * @param address			地址
	 * @return
	 */
	@Post("saveOnlineBooking")
	public String saveOnlineBooking(@Param("type") String type,@Param("beginAt") String beginAt,@Param("subPeriod") String subPeriod,
			@Param("subNum") String subNum,@Param("link") String link,@Param("phone") String phone,@Param("invoiceTitle") String invoiceTitle,
			@Param("city") String city,@Param("area") String area,@Param("street") String street,@Param("address") String address,
			@Param("tel") String tel,@Param("descs") String descs)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			Map rep = new HashMap();
			rep.put("type", type);
			rep.put("beginAt", beginAt);
			rep.put("subPeriod", subPeriod);
			rep.put("subNum", subNum);
			rep.put("link", link);
			rep.put("phone", phone);
			rep.put("tel", tel);
			rep.put("descs", descs);
			rep.put("invoiceTitle", invoiceTitle);
			rep.put("city", wuhanCity);
			rep.put("area", KDAddressAreaEnum.getValue(area).getValue());
			rep.put("street", KDAddressStreetEnum.getValue(street).getValue());
			rep.put("address", address);
			rep.put("prize", jsPaperPrize(type, subPeriod, subNum));
			rep.put("status", KDBookingStatusEnum.DCL.getValue());
			rep.put("deleteFlag", Constants.deleteFlag.deleteFlag_1);
			rep.put("createUser", "portal");
			rep.put("updateUser", "portal");
			
			kdBookingDAO.createKDBookingPaper(rep);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 计算报纸价格
	 * @param type
	 * @param subPeriod
	 * @param subNum
	 * @return
	 */
	private BigDecimal jsPaperPrize(String type,String subPeriod,String subNum)
	{
		/**
		 * 长江日报：一份 一年480元，半年240元。
		 * 武汉晚报：一份 一年216元，半年108元。
		 * 武汉晨报：一份 一年144元，半年72元。
		 */
		double prize = 0;
		if("0".equals(type)){
			//长江日报
			prize = 480*Double.parseDouble(subPeriod)*Integer.parseInt(subNum);
		}else if("1".equals(type)){
			//武汉晚报
			prize = 216*Double.parseDouble(subPeriod)*Integer.parseInt(subNum);
		}else if("2".equals(type)){
			//武汉晨报
			prize = 114*Double.parseDouble(subPeriod)*Integer.parseInt(subNum);
		}
		return new BigDecimal(prize);
	}
	
	/**
	 * 订报卡启报
	 * @param model
	 * @return
	 */
	@Get("")
	public String showBookingCard(Model model,@Param("msg")String msg)
	{
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		model.add("memu", "showBookingCard");
		model.add("msg", msg);
		model.add("currentDate", DateUtils.getNowTime(DateUtils.DATE_SMALL_STR));
		model.add("kdAddressAreaEnum", EnumSet.allOf(KDAddressAreaEnum.class));
		model.add("kdAddressStreetEnum", EnumSet.allOf(KDAddressStreetEnum.class));
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/kdgs/booking/bookingCard.vm";
	}
	
	@Post("submitBookingCard")
	public String submitBookingCard(@Param("cardNum")String cardNum, @Param("cardPwd")String cardPwd, @Param("link")String link, @Param("phone")String phone,
			@Param("area")String area, @Param("street")String street, @Param("address")String address,@Param("beginAt") String beginAt){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		int areaVal = KDAddressAreaEnum.getValue(area).getValue();
		int streetVal = KDAddressStreetEnum.getValue(street).getValue();
		
		List<Map> maps = kdBookingDAO.validateCard(cardNum);
		if(maps != null && maps.size() > 0){
			String pwd = maps.get(0).get("cardPwd")==null ? "" : maps.get(0).get("cardPwd").toString();
			if("".equals(pwd)){
				kdBookingDAO.saveBookingCard(KDInTypeEnum.WZ.getValue(),cardNum,cardPwd,link,phone,wuhanCity,areaVal,streetVal,address,beginAt);
				kdBookingDAO.updateStatus(cardNum);
			}else{
				if(pwd.equals(cardPwd)){
					kdBookingDAO.saveBookingCard(KDInTypeEnum.WZ.getValue(),cardNum,cardPwd,link,phone,wuhanCity,areaVal,streetVal,address,beginAt);
					kdBookingDAO.updateStatus(cardNum);
				}
				else{
					jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
				}
			}
			
			
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Post("checkCardNum")
	public String checkCardNum(@Param("cardNum")String cardNum)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		List<Map> maps = kdBookingDAO.validateCardNoStatus(cardNum);
		
		if(maps!=null && maps.size()>0)
		{
			jo.put("bzzl", maps.get(0).get("paperName"));
			jo.put("bkmc", maps.get(0).get("paperCardName"));
			jo.put("months", maps.get(0).get("months"));
			jo.put("status", maps.get(0).get("status"));
		}
		else
		{
			jo.put("bzzl", "");
			jo.put("bkmc", "");
		}
		
		return "@" + jo.toString();
	}
	
	
	/**
	 * 旧报回收
	 * @param model
	 * @return
	 */
	@Get("showOldBooking")
	public String showOldBooking(Model model,@Param("msg") String msg)
	{
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		model.add("memu", "showOldBooking");
		model.add("msg", msg);
		model.add("kdAddressAreaEnum", EnumSet.allOf(KDAddressAreaEnum.class));
		model.add("kdAddressStreetEnum", EnumSet.allOf(KDAddressStreetEnum.class));
		model.add("KDRecovePaperWeightEnum", EnumSet.allOf(KDRecovePaperWeightEnum.class));
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/kdgs/booking/oldBooking.vm";
	}
	
	/**
	 * 保存旧报回收
	 * @param link	联系人
	 * @param phone	联系电话
	 * @param city	市
	 * @param area	区
	 * @param street	街道
	 * @param address	地址
	 * @return
	 */
	@Post("saveOldBooking")
	public String saveOldBooking(@Param("link") String link,@Param("phone") String phone,
			@Param("city") String city,@Param("area") String area,@Param("street") String street,@Param("address") String address,
			@Param("weight") String weight,@Param("descs") String descs)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			Map rep = new HashMap();
			rep.put("inType", KDInTypeEnum.WZ.getValue());
			rep.put("link", link);
			rep.put("phone", phone);
			rep.put("city", wuhanCity);
			rep.put("area", KDAddressAreaEnum.getValue(area).getValue());
			rep.put("street", KDAddressStreetEnum.getValue(street).getValue());
			rep.put("address", address);
			rep.put("descs", descs);
			rep.put("weight", weight);
			rep.put("status", KDBookingStatusEnum.DCL.getValue());
			rep.put("deleteFlag", Constants.deleteFlag.deleteFlag_1);
			rep.put("createUser", "portal");
			rep.put("updateUser", "portal");
			
			kdBookingDAO.createKDOldBooking(rep);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 改址转投
	 * @param model
	 * @return
	 */
	@Get("showChangeBooking")
	public String showChangeBooking(Model model,@Param("msg") String msg)
	{
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		model.add("memu", "showChangeBooking");
		model.add("msg", msg);
		model.add("kdBookingPaperTypeEnum", EnumSet.allOf(KDBookingPaperTypeEnum.class));
		model.add("kdAddressAreaEnum", EnumSet.allOf(KDAddressAreaEnum.class));
		model.add("kdAddressStreetEnum", EnumSet.allOf(KDAddressStreetEnum.class));
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/kdgs/booking/changeBooking.vm";
	}
	
	/**
	 * 改址
	 * @param type
	 * @param link
	 * @param phone
	 * @param invoiceNo
	 * @param oldAddress
	 * @param newAddress
	 * @return
	 */
	@Post("saveChangeBooking")
	public String saveChangeBooking(@Param("type") String type,@Param("link") String link,@Param("phone") String phone,
			@Param("invoiceNo") String invoiceNo,@Param("oldArea") String oldArea,@Param("oldStreet") String oldStreet,
			@Param("oldAddress") String oldAddress,	@Param("newArea") String newArea,@Param("newStreet") String newStreet,
			@Param("newAddress") String newAddress,@Param("tel") String tel)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			Map rep = new HashMap();
			rep.put("type", type);
			rep.put("link", link);
			rep.put("phone", phone);
			rep.put("tel", tel);
			rep.put("invoiceNo", invoiceNo);
			rep.put("oldCity", wuhanCity);
			rep.put("oldArea", KDAddressAreaEnum.getValue(oldArea).getValue());
			rep.put("oldStreet", KDAddressStreetEnum.getValue(oldStreet).getValue());
			rep.put("oldAddress", oldAddress);
			rep.put("newCity", wuhanCity);
			rep.put("newArea", KDAddressAreaEnum.getValue(newArea).getValue());
			rep.put("newStreet", KDAddressStreetEnum.getValue(newStreet).getValue());
			rep.put("newAddress", newAddress);
			rep.put("status", KDBookingStatusEnum.DCL.getValue());
			rep.put("deleteFlag", Constants.deleteFlag.deleteFlag_1);
			rep.put("createUser", "portal");
			rep.put("updateUser", "portal");
			
			kdBookingDAO.createKDChangeBooking(rep);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 投诉建议
	 * @param model
	 * @return
	 */
	@Get("showTouSu")
	public String showTouSu(Model model,@Param("msg") String msg)
	{
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		model.add("memu", "showTouSu");
		model.add("msg", msg);
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/kdgs/booking/touSu.vm";
	}
	
	/**
	 * 投诉建议
	 * @param msg
	 * @return
	 */
	@Post("saveAdvise")
	public String saveAdvise(@Param("msgBox") String msgBox,@Param("link") String link,@Param("phone") String phone)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			Map rep = new HashMap();
			rep.put("inType", KDInTypeEnum.WZ.getValue());
			rep.put("link", link);
			rep.put("phone", phone);
			rep.put("msg", msgBox);
			rep.put("status", KDBookingStatusEnum.DCL.getValue());
			rep.put("deleteFlag", Constants.deleteFlag.deleteFlag_1);
			rep.put("createUser", "portal");
			rep.put("updateUser", "portal");
			
			kdBookingDAO.createKDAdvise(rep);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 集团down
	 * @param category
	 * @return
	 */
	private List<Map> jtpphd(String category)
	{
		//集团品牌活动
		List<Map> ppls = lineDAO.query(category, 0, 5);
		List<Map> pphdls = new ArrayList<Map>();
		for(Map rep :ppls)
		{
			Map l_rep = new HashMap();
			l_rep.put("id", rep.get("id"));//ID
			String title=rep.get("title").toString();
			if(title.length()>15)
			{
				title=title.toString().substring(0, 15)+"...";
			}
			l_rep.put("title", title);//标题
			l_rep.put("abstractInfor", rep.get("abstractInfor"));//简介
			//缩率图
			String carouselImage = rep.get("carouselImage")==null?"":rep.get("carouselImage").toString();
//			if(carouselImage !=null && !"".equals(carouselImage))
//			{
//				String carouselImages[] = carouselImage.split("/");
//				l_rep.put("carouselImage", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
//			}
//			else
//			{
//				l_rep.put("carouselImage", "");//缩率图
//			}
			l_rep.put("carouselImage", carouselImage);//缩率图
			pphdls.add(l_rep);
		}
		return pphdls;
	}
}
